package sysmgr.dao.impl;

import commmon.db.DbConnection;
import commmon.db.JdbcConnectionPool;
import sysmgr.bean.Role;
import sysmgr.bean.Staff;
import sysmgr.dao.StaffDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StaffDaoImpl implements StaffDao {
    public int insertStaff(Staff staff) {
        return 0;
    }

    @Override
    public int udpateStaff(Staff staff) {
        return 0;
    }

    @Override
    public int deleteStaff(Staff staff) {
        return 0;
    }

    @Override
    public int updateStaff(Staff staff) {
        return 0;
    }

    @Override
    public List<Staff> queryStaff(Staff staff) {
        return null;
    }

    @Override
    public Staff staffLogin(Staff staff){
        try {
            Connection conn= JdbcConnectionPool.getConnection();
            String sql="select * from t_staff a where a.username=?";
            String param []={staff.getUserName()};
            ResultSet q = (ResultSet) DbConnection.toExecuteMethod(conn, sql, param, "Q");
            Staff staff1= (Staff) DbConnection.getObjectFromResultSet(q,staff.getClass());
            if(staff1!=null){
                return staff1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
        @Override
        public List<Role> queryRole(int role) {
            List<Role> roles = new ArrayList<>();
            try {
                Connection coon = JdbcConnectionPool.getConnection();
                String sql1 = "SELECT r.* FROM t_role r,t_staff_role sr where r.id=sr.roleid and sr.staffid = ?";
                PreparedStatement preparedStatement = coon.prepareStatement(sql1);
                preparedStatement.setInt(1, role);
                ResultSet resultSet = preparedStatement.executeQuery();
                while (resultSet.next()) {
                    Role t_role = new Role(resultSet.getInt("id"), resultSet.getString("rolename"), resultSet.getString("createdate"));
                    roles.add(t_role);
                }
                return roles;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

